package yki.paperplane.server.db.planeoperator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import yki.paperplane.server.db.DBOperator;
import yki.paperplane.server.model.Reply;

public class GetTheLastReply extends DBOperator{

	int session_id;
	
	public GetTheLastReply( int sid ){
		session_id = sid;
	}
	
	@Override
	public Reply execute(Connection connection) throws SQLException {
		PreparedStatement statement = connection.prepareStatement( SQL_GETTHELASTREPLY);
		statement.setInt( 1, session_id);
		ResultSet resultSet = statement.executeQuery();
		if( resultSet.wasNull() )
			return null;
		
		Reply reply = new Reply();
		reply.setMessage( resultSet.getString( MESSAGE));
		reply.setDate( resultSet.getDate( DATE));
		reply.setReply_id( resultSet.getInt( REPLY_ID));
		reply.setState( resultSet.getInt( REPLY_STATE));
		reply.setSession_id( resultSet.getInt( SESSION_ID));
		
		return reply;
	}

}
